/** @format */

import { loadEnv } from 'vite';

/**
 * @envPrefix 配置环境变量前缀默认 VITE_
 * @envDir 配置环境变量路径，默认为项目根目录
 * @title 网站标题
 */

export default (opthons?: any): any => {
  return {
    config(config: any, env: any) {
      console.log('---vite-plugin-vue-guoguo-config');
      // console.log('opthons?.envDir ===>', opthons)
      loadEnv(env.mode, `${process.cwd()}`, opthons?.envPrefix || '');

      // console.log('😁😁😁config ===>', config);
      // console.log('😁😁😁env ===>', env);

      return {
        envDir: opthons?.envDir || '',
      };
    },
    transformIndexHtml(html: any) {
      if (!!opthons.title) {
        return html.replace(
          /<title>(.*?)<\/title>/,
          `<title>${opthons.title}</title>`,
        );
      }
      return null;
    },
    configResolved(config: any) {
      // console.log('configResolved ===>', config);
    },
  };
};
